#include<iostream>
#include<vector>
#include<string>
#include<unordered_set>
using namespace std;



class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_set<char> hash;
        int len = s.size();
        int left = 0;
        int right = 0;
        int maxLen = 0;
        while (left < len) {
            while (right < len && !hash.count(s[right])) {
                hash.insert(s[right]);
                right++;
            }
            if (right < len && hash.count(s[right])) {
                maxLen = max(maxLen, right - left);
                while (left < len && hash.count(s[right])) {
                    hash.erase(s[left]);
                    left++;
                }
            }
            else {
                maxLen = max(maxLen, right - left);
                break;
            }
        }
        return maxLen;
    }
};